/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui.playlist;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingWorker;

import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventTopicSubscriber;
import org.jdesktop.swingx.JXPanel;
import org.shake.lastfm.event.EventTopic;
import org.shake.lastfm.event.FetchPlaylistRequestEvent;
import org.shake.lastfm.event.SessionStartedEvent;
import org.shake.lastfm.net.LastFmCommunicator;
import org.shake.lastfm.swing.SwingJob;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class UrlPanel extends JXPanel
{
    private static final long serialVersionUID = 1549469588747569195L;
    
    private JComboBox urlField;
    private AbstractAction tuneInAction;
    
    public UrlPanel()
    {
        super();
        
        buildUI();
        AnnotationProcessor.process( this );
    }

    private void buildUI()
    {
        FormLayout layout = new FormLayout(
                "p:grow, 1dlu, min",
                "p"
        );
        CellConstraints cc = new CellConstraints();
        setLayout(layout);
        
        this.tuneInAction = new TuneInAction();
        JButton tuneButton = new JButton(this.tuneInAction);
        this.urlField = new JComboBox();
        this.urlField.setEditable(true);
        
        add(this.urlField, cc.xy(1, 1));
        add(tuneButton, cc.xy(3, 1));
    }
    
    @EventTopicSubscriber(topic = EventTopic.SESSION_STARTED_EVENT)
    public void onSessionStarted(String topic, SessionStartedEvent event)
    {
        this.tuneInAction.setEnabled(true);
    }
    
    public void changeStation(final SwingJob<String> swingJob)
    {
        final String station = swingJob.readUI();
        new SwingWorker<String, Void>()
        {
            @Override
            protected String doInBackground() throws Exception
            {
                LastFmCommunicator.getInstance().changeStation(station);
                return null;
            }
            
            @Override
            protected void done()
            {
                try
                {
                    swingJob.onJobDone( get() );
                } catch (Exception e)
                {
                    swingJob.onJobCanceled( e );
                }
            }
            
        }.execute();
    }
    
    private class TuneInAction extends AbstractAction
    {
        private static final long serialVersionUID = 1L;
        
        public TuneInAction()
        {
            super(">");
            setEnabled(false);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            changeStation(new ChangeStationJob());
        }
    }
    
    private class ChangeStationJob implements SwingJob<String>
    {
        @Override
        public void onJobCanceled(Exception ex)
        {
        }

        @Override
        public void onJobDone(String data)
        {
            EventBus.publish(EventTopic.PLAYLIST_FETCH_REQUEST, new FetchPlaylistRequestEvent(false));
        }

        @Override
        public String readUI()
        {
            Object selectedItem = UrlPanel.this.urlField.getSelectedItem();
            return selectedItem != null ? selectedItem.toString() : null;  
        }
        
    }

}